package com.aigou.service;

import com.aigou.dao.BrandMapper;
import com.aigou.dao.GoodsMapper;
import com.aigou.dao.UserMapper;
import com.aigou.exception.AigouException;
import com.aigou.pojo.*;
import com.aigou.pojo.GoodsExample.Criteria;
import com.aigou.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    BrandMapper brandMapper;

    public List<Goods> findByIds(List<Integer> ids) {
    	GoodsExample ex = new GoodsExample();
    	Criteria criteria = ex.createCriteria();
    	criteria.andIdIn(ids);
		return goodsMapper.selectByExample(ex);
    }
    
    public PageInfo<Goods> findAll(Integer page
            , Integer size, Goods goods) {
        PageHelper.startPage(page, size);
        GoodsExample ex = new GoodsExample();
        GoodsExample.Criteria criteria = ex.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        if (!StringUtils.isEmpty(goods.getName())) {
            criteria.andNameLike("%" +goods.getName()+ "%");
        }
        return new PageInfo<Goods>(goodsMapper.selectByExample(ex));
    }

    public Goods findOne(Integer id) {
        if (id == null) {
            throw new AigouException("请输入id");
        }
        Goods goods = goodsMapper.selectByPrimaryKey(id);
//        Goods goods = goodsMapper.selectById(id);
//        if (goods.getIsDelete().equals(1)) {
//            throw new AigouException("商品不存在");
//        }
        return goods;
    }

    public void add(Goods goods) {
        goods.setIsDelete(0);
        goods.setStatus(0);
        goods.setIsShangjia(1);
        Date date = new Date();
        goods.setCreateTime(date);
        goods.setUpdateTime(date);
        goods.setStatus(0);

        Brand brand = brandMapper.selectByPrimaryKey(goods.getBrand());
        goods.setBrandName(brand.getName());

        goodsMapper.insertSelective(goods);
    }

    @Transactional
    public void update(Goods goods) {
        Date date = new Date();
        goods.setUpdateTime(date);
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    public void delete(Integer id) {
        Goods goods = new Goods();
        Date date = new Date();
        goods.setUpdateTime(date);
        goods.setId(id);
        goods.setIsDelete(1);
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Transactional
    public void addKuCun(Integer gid, int num) {

        Goods goods = this.findOne(gid);
        updateNum(gid, goods.getNum() + num, goods.getVersion());

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    public void updateNum(Integer gid, int num, Long version) {
        int update = goodsMapper.updateNum(gid, num, version);
        if (update < 1) {
            throw new AigouException("更新商品数量失败");
        }
    }
}
